package cn.edu.sdyu.ie.crs.api;

import cn.edu.sdyu.ie.crs.common.response.PageVO;
import cn.edu.sdyu.ie.crs.common.response.Result;
import cn.edu.sdyu.ie.crs.domain.Term;
import cn.edu.sdyu.ie.crs.service.TermService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.Parameter;
//import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@Tag(name = "学期管理", description = "学期信息管理接口")
@Slf4j
@RestController
@RequestMapping("api/term")
public class TermController {

    @Autowired
    private TermService termService;

    /**
     * 添加新学期信息
     *
     * @param term
     * @return
     */
    @Operation(tags = "学期管理", summary = "添加学期信息")
    @PostMapping("")
    public Result<Void> addTerm(Term term) {

        if (term.getId() != null && !term.getId().equals(0)) {
            log.warn("请求ID参数，若更新对象，请使用PUT方式发起请求。");
            return Result.failure();
        }
        termService.saveTerm(term);
        return Result.success();
    }

    /**
     * 更新学期信息
     *
     * @param term
     * @return
     */
    @Operation(tags = "学期管理", summary = "修改学期信息")
    @PutMapping("")
    public Result<Object> updateTerm(Term term) {

        if (term.getId() == null || term.getId().equals(0)) {
            log.warn("未请求ID参数，使用POST方式创建对象。当前不会更新任何对象");
            return Result.failure();
        }
        termService.saveTerm(term);
        return Result.success();
    }

    /**
     * 删除指定ID的学期信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "学期管理", summary = "删除学期信息")
    @DeleteMapping("/{id}")
    public Result<Void> deleteTermById(@PathVariable Integer id) {
        termService.deleteTermById(id);
        return Result.success();
    }

    /**
     * 查询指定ID的学期信息
     *
     * @param id
     * @return
     */
    @Operation(tags = "学期管理", summary = "查询指定ID的学期信息")
//    @Parameters(value = {
//            @Parameter(description = "教室ID")
//    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学期ID")
    })
    @GetMapping("/{id}")
    public Result<Term> findTermById(/*@Parameter(description = "学期表ID")*/ @PathVariable Integer id) {
        Term term = termService.findTermById(id);

        if (term == null) {
            return Result.failure();
        }

        return Result.success(term);
    }


    /**
     * 分页查询学期信息
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(tags = "学期管理", summary = "分页查询学期信息")
    @GetMapping("")
    public Result<PageVO<Term>> listTermsPage(@RequestParam(defaultValue = "1") Integer page,
                                                        @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<Term> list = termService.findByPage(page - 1, pageSize);
        PageVO<Term> pageVO = PageVO.getPageVO(list);
        return Result.success(pageVO);
    }
}